<template>
  <view class="main">
    <text>基本用户信息</text>
    <uni-forms ref="editForm" :modelValue="editFormData">
      <uni-forms-item label="用户昵称">
        <uni-easyinput v-model="editFormData.nickName" placeholder="请输入昵称" />
      </uni-forms-item>
      <uni-forms-item label="用户生日">
        <uni-datetime-picker type="datetime" return-type="timestamp" v-model="editFormData.brith" />
      </uni-forms-item>
      <uni-forms-item label="用户性别">
        <uni-data-checkbox v-model="editFormData.sex" :localdata="sexs" />
      </uni-forms-item>
      <uni-forms-item label="用户头像">
        <uni-file-picker v-model="imageValue" fileMediatype="image" limit="1" mode="grid" ref="files" :auto-upload="false" @success="uploadSuccess" />
      </uni-forms-item>

    </uni-forms>
    <button type="primary" @click="submitData">确认</button>

  </view>
</template>

<script>
  export default {
    data() {
      return {
        editFormData: {},
        // 单选数据源
        sexs: [{
          text: '男',
          value: 0
        }, {
          text: '女',
          value: 1
        }, {
          text: '保密',
          value: 2
        }],
        token: '',
        imageValue: [],
        picurls: []
      };
    },
    onShow() {
      try {
        this.token = uni.getStorageSync('token')
        this.getUserData()
      } catch (e) {}
    },
    methods: {
      getUserData() {
        if (this.token) {
          uniCloud.callFunction({
            name: 'user_get_info',
            data: {
              id: this.token
            }
          }).then(res => {
            this.editFormData = res.result.data[0];
            // 处理图片格式
            this.imageValue = this.editFormData.userPic.map(item => {
              return {
                url: item
              }
            })
          })

        }
      },
      uploadSuccess(e) {
        this.picurls = e.tempFilePaths
      },
      // 更新用户信息
      async submitData() {
        await this.$refs.files.upload()
        if (this.picurls.length > 0) {
          this.editFormData.userPic = this.picurls
        }
        uniCloud.callFunction({
          name:"user_update",
          data:{
            id:this.token,
            editFormData:this.editFormData,
          }
        }).then(res =>{
          console.log(res);
          uni.showToast({
          	title:"修改信息成功"
          })
        })
      }

    }
  }
</script>

<style lang="scss" scoped>
  .main {
    margin: 0 40rpx;
    padding-top: 40rpx;
    
    >text {
      background-color: #fff;
    }
  }

  .uni-forms {
    border: 4rpx solid #067fcf;
    margin-bottom: 44rpx;
    padding-top: 44rpx;
    background-color: #fff;
  }

  .uni-forms-item {
    width: 640rpx;
  }

  /deep/.uni-forms-item__label {
    width: 150rpx !important;
  }
</style>
